﻿namespace Controllers
{
    using Domains;
    using System.IO;
    using System.Collections.Generic;
    using System;

    /// <summary>
    /// Controller for load input files
    /// </summary>
    public static class LoadInputFilesController
    {
        /// <summary>
        /// Load input files and clear temp files
        /// </summary>
        /// <param name="previusVersion">The name of the file with previus version</param>
        /// <param name="customVersion">The name of the file with customer version</param>
        /// <param name="nextVersion">The name of the file with next version</param>
        public static void Load(string previusVersion, string customVersion, string nextVersion, string beyondpath)
        {
            DeleteTempFiles();
            Customer = customVersion;
            Previus = previusVersion;
            Next = nextVersion;
            Execution.beyondpath = beyondpath;

            string PathToBComp = beyondpath.Remove(beyondpath.Length - 7, 3);
            PathToBComp = "SET BC3_PROGRAM=" + PathToBComp;
            StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Scripts\\MergeCommands.bat");
            List<string> NewFile = new List<string>();
            while (!reader.EndOfStream)
            {
                string tmpstring = reader.ReadLine();
                if (tmpstring.Contains("BComp.exe"))
                {
                    tmpstring = PathToBComp;
                }
                NewFile.Add(tmpstring);
            }
            reader.Close();
            StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Scripts\\MergeCommands.bat");
            foreach (var item in NewFile)
            {
                writer.WriteLine(item);
            }
            writer.Close();
        }

        #region Private Methods

        /// <summary>
        /// Deleting the files which program use for compare and merge
        /// </summary>
        private static void DeleteTempFiles()
        {
            Execution.CheckAndDelete(@"CustomerPrevius.html");
            Execution.CheckAndDelete(@"CustomerNext.html");
            Execution.CheckAndDelete(@"PreviusNext.html");
            Execution.CheckAndDelete(@"Customer.html");
            Execution.CheckAndDelete(@"Previus.html");
            Execution.CheckAndDelete(@"Next.html");
            Execution.CheckAndDelete(@"TempResult.html");
            Execution.CheckAndDelete(@"CustomeResult.html");
            Execution.CheckAndDelete(@"PreviusResult.html");
            Execution.CheckAndDelete(@"NextResult.xml");
        }

        #endregion

        #region Properties

        /// <summary>
        /// Full path to Custom Version File
        /// </summary>
        public static string Customer { get; set; }

        /// <summary>
        /// Full path to Previus Version File
        /// </summary>
        public static string Previus { get; set; }

        /// <summary>
        /// Full path to Next Version File
        /// </summary>
        public static string Next { get; set; }

        #endregion
    }
}
